﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels/UserManagementViewModel
* 类       名: UserManagementViewModel
* 功      能:  TD
* 时      间:  2024-04-29 17:56:07
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.DAL.LocalDataAccess;
using WaferSelectV3.Dialogs;
using WaferSelectV3.Models;
using WaferSelectV3.Services;

namespace WaferSelectV3.ViewModels
{
    public class UserManagementViewModel : BindableBase, INavigationAware
    {

        public UserManagementViewModel()
        {
            UserOperateCommand = new DelegateCommand<string>(DoUserOperate);
        }

        public DelegateCommand<string> UserOperateCommand { get; private set; }
        private void DoUserOperate(string parameter)
        {
            DialogParameters parameters;
            switch (parameter.ToLower())
            {
                case "add":
                    parameters = new DialogParameters()
                    {
                        { "Title","添加用户"}
                    };
                    DialogProvider.Show(nameof(AddUserDialog), parameters);
                    break;
                case "edite":
                    if (SelectedIndex == -1)
                    {
                        MessageBox.Show("你先双击选中你要编辑的用户", "用户未选中提示框", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    parameters = new DialogParameters()
                    {
                        {"Title","编辑用户" },
                        {"UserInfo",UserInfoList![SelectedIndex] }
                    };
                    DialogProvider.Show(nameof(EditeUserDialog), parameters);
                    break;
                case "delete":
                    if (SelectedIndex == -1)
                    {
                        MessageBox.Show("你先双击选中你要编辑的用户", "用户未选中提示框", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    DataResult result = UserAccess.DeleteSingleDataById(UserInfoList![SelectedIndex].Id);
                    if (!result.State)
                    {
                        MessageBox.Show(result.Message!, "数据删除失败提示框", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    break;
                default:
                    break;
            }
            ReloadUserInfos();
        }


        private ObservableCollection<UserModel>? _userInfoList;

        public ObservableCollection<UserModel>? UserInfoList
        {
            get { return _userInfoList; }
            set
            {
                _userInfoList = value;
                RaisePropertyChanged();
            }
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = value;
                RaisePropertyChanged();
            }
        }



        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            UserInfoList?.Clear();
            UserInfoList = null;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            SelectedIndex = -1;
            // 数据初始化
            ReloadUserInfos();
        }

        private void ReloadUserInfos()
        {
            UserInfoList = new ObservableCollection<UserModel>();
            var result = UserAccess.GetUsers();
            if (result.State)
            {
                if (result.Data!.Count == 0)
                {
                    DataResult initResult = UserAccess.DataInit();
                    if (!initResult.State)
                    {
                        MessageBox.Show(initResult.Message, "数据库操作异常", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        result = UserAccess.GetUsers();
                        if (!result.State)
                        {
                            MessageBox.Show(result.Message, "数据库操作异常", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }

                foreach (var entity in result.Data!)
                {
                    var model = new UserModel()
                    {
                        Id = entity.Id,
                        Username = entity.UserName,
                        Password = entity.Password,
                        Role = entity.Role,
                        CreateBy = entity.CreateBy,
                        CreateTime = entity.CreateTime,
                        LastLoginTime = string.IsNullOrWhiteSpace(entity.LastLoginTime) ? "无" : entity.LastLoginTime
                    };
                    UserInfoList.Add(model);
                }
            }
            else
            {
                MessageBox.Show(result.Message, "数据库操作异常", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
